/*
 * Copyright (c) 2012 Universidad Antonio de Nebrija.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License v2.0
 * which accompanies this distribution, and is available at
 * http://www.apache.org/licenses/LICENSE-2.0.html
 * 
 * Contributors:
 *    Universidad Antonio de Nebrija - initial API, implementation and documentation
 */
package com.nebrija.riif.cli;

import java.io.IOException;
import java.util.ArrayList;

import org.antlr.runtime.tree.Tree;

import com.nebrija.riif.model.RootClass;
import com.nebrija.riif.parser.ASTRIIFConverter;
import com.nebrija.riif.parser.ASTRIIFLoader;

/**
 * Command to load a RIIF file 
 * @author asanchep
 *
 */
public class LoadCommand implements Command{

	@Override
	public String execute(ArrayList<String> params) throws CommandException{
		if (params.size()==0){
			throw new CommandException (CommandException.NOT_ENOUGH_PARAMS);
		}
		
		// Get the file name, load the file and build the model
		String filename = params.get(0);
		ASTRIIFLoader loader = new ASTRIIFLoader();
		try{
			Tree t = loader.loadRIIF(filename);
			ASTRIIFConverter converter = new ASTRIIFConverter();
			RootClass r = RootClass.getInstance();
			converter.processAST(t,r);
			// We return the root class to print the current information, but could return an empty string
			//return r.toString(); 
			return "File was loaded\n";
		}catch (IOException e) {
			throw new CommandException (e.getMessage());
		}
	}

	@Override
	public String helpString() throws CommandException {
		return " <filename>  :  load a RIIF file";
	}

	
}
